// // 微信阅读  美添赚2


if (!requestScreenCapture()) {
    toastLog("请求截图权限失败");
    exit();
}


upapp(0)
taskCenter()
SignIning()
tx(11031140, 8888, 2)
readnew()
gzhgz()



// // 启动app
function upapp(n) {
    toastLog("开始启动-微信")
    home()
    sleep(random(3000, 5000))
    launchApp("微信")
    sleep(random(3000, 5000))
    if (text("使用以下方式打开").exists()) {
        var like = text("微信").findOnce().parent().parent().child(n)
        randomClick(like)
        sleep(random(3000, 5000))
    }
}
// 签到
function SignIning() {
    toastLog("开始签到")
    ocrc("我的主页", 785, 1916, 1015, 2032)
    let resultText = ocrf(770, 1350, 1000, 1550)
    if (resultText.includes("点击领取")) {
        ocrc("点击领取", 770, 1350, 1000, 1550)
        toastLog("任务已完成，开始退出");
        cck(472, 1595, 607, 1727)
        ocrc("互助活动", 423, 1916, 656, 2032)
    } else {
        ocrc("互助活动", 423, 1916, 656, 2032)
    }
}

// //收藏夹阅读
function taskCenter() {
    if (currentActivity() == "com.tencent.mm.plugin.webview.ui.tools.MMWebViewUI") {
        toastLog("在任务主页面")
        sleep(random(3000, 5000))
    } else {
        let maxRetry = 5;
        while (currentActivity() != "com.tencent.mm.ui.LauncherUI" && maxRetry-- > 0) {
            toastLog("微信启动中，请稍等。。。")
            sleep(random(5000, 10000))
        }
        if (currentActivity() == "com.tencent.mm.ui.LauncherUI") {
            toastLog("微信启动完成")
            sleep(random(3000, 5000))
        }

        if (currentActivity() == "com.tencent.mm.ui.LauncherUI") {
            toastLog("发现微信主页-点击我")
            // ocrc("我", 810, 1889, 1080, 2040)
            // ocrc("收藏", 151, 849, 243, 911)
            cck(810, 1889, 1080, 2040)//我
            sleep(random(3000, 5000))
            cck(0, 804, 1080, 956)//收藏
            sleep(random(3000, 5000))
        }
        if (currentActivity() == "com.tencent.mm.plugin.fav.ui.FavoriteIndexUI") {
            toastLog("发现我的收藏")
            // ocrc("推广码", 0, 482, 1080, 958)
            cck(0, 482, 1080, 958)//推广码
            sleep(random(3000, 5000))
            sleep(random(3000, 5000))
        }
        if (currentActivity() == "com.tencent.mm.plugin.fav.ui.detail.FavoriteImgDetailUI") {
            toastLog("发现详情")
            sleep(random(3000, 5000))
            // ocrl("推广码", 65, 229, 1015, 1648)
            longCk(65, 229, 1015, 1648)//推广码
            sleep(random(3000, 5000))
        }
        if (currentActivity() == "com.tencent.mm.ui.widget.dialog.w3") {
            toastLog("发现识别图中二维码")
            // ocrc("识别图中的二维码", 0, 1715, 1080, 1866)
            cck(0, 1715, 1080, 1866)//识别图中的二维码
            sleep(random(3000, 5000))
        }
        if (currentActivity() == "com.tencent.mm.plugin.webview.ui.tools.MMWebViewUI") {
            toastLog("继续访问 同意并继续使用 主界面")
            ocrc("继续访问", 288, 1616, 791, 1751)
            ocrc("同意并继续使用", 199, 1676, 880, 1813)
            sleep(random(5000, 10000))
        }
    }
}

// text("文章阅读推荐")
function readnew() {
    var refPos = ocrc("文章阅读推荐", 224, 820, 540, 1649, false);

    if (refPos) {
        // 计算"开始活动"的搜索区域
        // 基于参考位置：向右150，向下0，宽度250，高度100
        var startX = refPos.x + 540;  // 参考点X + 150
        var startY = refPos.y;        // 参考点Y
        var endX = startX + 250;      // 宽度250
        var endY = startY + 150;      // 高度100

        // toastLog("搜索'开始活动'区域: " + startX + "," + startY + " - " + endX + "," + endY);
        toastLog("搜索'开始活动'");
        // 第二步：在计算出的区域内识别"开始活动"
        var refPos = ocrc("开始活动", startX, startY, endX, endY, false);
        if (refPos) {
            var refPos = ocrc("点击复制广告", 394, 982, 685, 1063, false)
            if (refPos) {
                sleep(random(5000, 10000))
                // 二维码范围
                longCk(380, 858, 699, 1176)

                // if (currentActivity() == "com.tencent.mm.ui.widget.dialog.w3") {
                //     toastLog("发现识别图中二维码")
                //     ocrc("青团设计", 0, 1715, 1080, 1866)
                // }

                ocrc("青团设计", 0, 1715, 1080, 1866)

                // 未关注青团设计
                if (currentActivity() == "com.tencent.mm.plugin.profile.ui.ContactInfoUI") {
                    toastLog("关注青团设计公众号")
                    ocrc("关注公众号", 54, 593, 1026, 701)
                    ocrc("发消息", 54, 593, 1026, 701)
                }

                toastLog("点击输入框粘贴发送")
                sleep(random(3000, 5000));
                longCk(130, 1910, 842, 2018)
                sleep(random(3000, 5000));
                var refPos = ocrc("粘贴", 50, 850, 150, 1910)
                if (refPos) {
                } else {
                    longCk(130, 1910, 842, 2018)
                    sleep(random(3000, 5000));
                    ocrc("粘贴", 50, 850, 150, 1910)
                }
                ocrc("发送", 896, 1087, 1058, 2008)
                ocrc("点我开始阅读", 570, 200, 920, 1870, true);
                ocrc("继续访问", 288, 1616, 791, 1751)

                for (var i = 0; i < 50; i++) {
                    sleep(random(10000, 15000));
                    // 调用OCR函数并处理结果
                    let resultText = ocrf(383, 679, 696, 990);
                    if (resultText.includes("已完成")) {
                        toastLog("任务已完成，开始退出");
                        back();
                        sleep(random(3000, 5000));
                        back();
                        sleep(random(3000, 5000));
                        back();
                        sleep(random(3000, 5000));
                        break
                    } else if (resultText.includes("（") && resultText.includes("/") && resultText.includes("）")) {
                        toastLog("正在加载。。。");
                        sleep(random(5000, 10000));
                        // } else if (ocrc("我知道了", 108, 1579, 972, 1749)) {
                        //     toastLog("系统检测到你已失效")
                        //     back();
                        //     sleep(random(3000, 5000));
                        //     back();
                        //     sleep(random(3000, 5000));
                        //     back();
                        //     sleep(random(3000, 5000));
                        //     break
                    } else {
                        toastLog("在阅读页面，开始返回");
                        sleep(random(3000, 5000));
                        back();
                        sleep(random(8000, 10000));
                    }
                    toastLog("第-" + (i + 1) + "-次阅读完成")
                }
            } else {
                toastLog("可能没有任务了");
            }
        } else {
            ocrf(startX, startY, endX, endY)
        }
    } else {
        toastLog("未找到'文章阅读推荐'，无法继续");
    }


}

// text("公众号关注推荐")
function gzhgz() {
    var username = ocrf(50, 200, 400, 280)
    if (username.match(/\d+/g) == null) {
        toastLog("用户名符合条件，开始关注")
        for (var i = 0; i < 30; i++) {
            var refPos = ocrc("公众号关注推荐", 224, 820, 540, 1649, false);
            if (refPos) {
                // 计算"开始活动"的搜索区域
                // 基于参考位置：向右150，向下0，宽度250，高度100
                var startX = refPos.x + 540;  // 参考点X + 150
                var startY = refPos.y;        // 参考点Y
                var endX = startX + 250;      // 宽度250
                var endY = startY + 150;      // 高度100

                // toastLog("搜索'开始活动'区域: " + startX + "," + startY + " - " + endX + "," + endY);
                toastLog("搜索'开始活动'");
                // 第二步：在计算出的区域内识别"开始活动"
                var refPos = ocrc("开始活动", startX, startY, endX, endY, false);
                if (refPos) {
                    let resultText = ocrf(350, 1160, 730, 1300);
                    if (resultText.includes("长按识别前往关注")) {
                        longCk(380, 879, 699, 1201)
                        sleep(random(3000, 5000));
                        ocrc("前往图中包含的公众号", 0, 1618, 1080, 1867);
                        ocrc("关注", 400, 600, 650, 900);
                        sleep(random(3000, 5000));
                        // ocrc("发消息", 732,684,846,736);
                        let resultText = ocrf(700, 600, 900, 900);
                        if (resultText.includes("发消息")) {
                            sleep(random(3000, 5000));
                            back()
                            sleep(random(3000, 5000));
                            ocrc("本次已关注", 221, 893, 858, 1066);
                        } else {
                            back()
                            sleep(random(3000, 5000));
                            let resultText = ocrf(700, 600, 900, 900);
                            if (resultText.includes("发消息")) {
                                sleep(random(3000, 5000));
                                back()
                                sleep(random(3000, 5000));
                                ocrc("本次已关注", 221, 893, 858, 1066);

                            } else {
                                ocrc("确定", 400, 1100, 700, 1250)
                                back()
                                sleep(random(3000, 5000));
                                ocrc("未关注", 221, 1133, 858, 1309);
                            }
                        }

                    } else {
                        toastLog("可能没有任务了");
                        break
                    }
                } else {
                    ocrf(startX, startY, endX, endY)
                    break
                }
                toastLog("第-" + (i + 1) + "-次关注完成");
            } else {
                toastLog("未找到'公众号关注推荐'，无法继续");
            }
        }
    }
}


function tx(idh, mm, money) {

    if (ocrf(499, 400, 900, 477).match(/\d+/g)[0] >= money) {
        var userid = ocrf(50, 270, 450, 340).match(/\d+/g)
        // toastLog(userid)
        var username = ocrf(50, 200, 400, 280)
        ocrc("兑换", 866, 415, 1026, 485)
        if (username.match(/\d+/g)) {
            toastLog("开始提现")
            ocrc("提现到微信", 118, 698, 961, 1206)


        } else {
            toastLog("积分转移")
            ocrc("积分转移", 437, 1249, 591, 1298)
            ocrc("积分转移", 450, 1000, 640, 1150)

            // var resultText = ocrf(100, 1020, 520, 1200);
            // if (resultText.includes("请输入对方ID")) {
            //     ocrc("请输入对方ID", 100, 1020, 520, 1200);
            //     setClip(idh)
            //     sleep(random(3000, 5000))
            //     paste()
            //     sleep(random(3000, 5000))

            //     ocrc("查询昵称", 520, 1020, 1000, 1200)
            //     ocrc("确认绑定", 520, 1300, 1000, 1450)
            // }

        }

        ocrc("输入用户ID", 400, 950, 650, 1050)
        setClip(userid)
        sleep(random(3000, 5000))
        paste()
        sleep(random(3000, 5000))
        var resultText = ocrf(400, 950, 650, 1050);
        if (resultText.includes("输入用户ID")) {
            setClip(userid)
            sleep(random(3000, 5000))
            paste()
            sleep(random(3000, 5000))
        }
        ocrc("输入提现密码", 400, 700, 650, 1250)
        setClip(mm)
        sleep(random(3000, 5000))
        paste()
        sleep(random(3000, 5000))
        var resultText = ocrf(400, 700, 650, 1250)
        if (resultText.includes("输入提现密码")) {
            setClip(userid)
            sleep(random(3000, 5000))
            paste()
            sleep(random(3000, 5000))
        }


        ocrc("确认", 400, 700, 650, 1250)
        ocrc("我知道了", 400, 1450, 650, 1600)
    }


}

// cck(472, 1595, 607, 1727)
// ocrf( 472,1595,607,1727)
// ocrc("我知道了",  400, 950, 650, 1050)





























// ocrc("我")  bounds("(929,1981,961,2024)")text("我")bounds("(810,1889,1080,2040)")
// ocrc("收藏")  bounds("(151,849,243,911)")text("收藏")bounds("(0,804,1080,956)")
// ocrc("推广码")bounds("(0,482,1080,958)")推广码
// ocrl("推广码")bounds("(65,229,1015,1648)")
// ocrc("识别图中的二维码")bounds("(356,1759,724,1821)xxxx不准xxxx")text("识别图中的二维码")bounds("(0,1715,1080,1866)")
// ocrc("继续访问")bounds("(288,1616,791,1751)")text("继续访问")className("android.widget.Button")
// ocrc("同意并继续使用")bounds("(199,1676,880,1813)")text("同意并继续使用")className("android.widget.Button")
// ocrc("公告通知")bounds("(229,1541,850,1654)")text("我知道了")
// 签到
// ocrc("我的主页")bounds("(785,1916,1015,2032)")text("我的主页")
// ocrc("点击领取")bounds("(774,1465,996,1535)")text("点击领取")
// ocrc("x")bounds("(472,1595,607,1727)") clickable("true") x




// 做任务
// bounds("(67,1020,1012,1233)")文章阅读推荐
// ocrc("互助活动")bounds("(423,1916,656,2032)")text("互助活动")
// ocrc("开始活动")bounds("(224,1030,486,1090)")text("文章阅读推荐")bounds("(764,1082,1012,1171)")text("开始活动")
// ocrc("点击复制广告")bounds("(394,982,685,1063)")text("点击复制广告")
// bounds("(353,1198,726,1260)")text("务必观看视频教程")
// longClick(x, y)二维bounds("(380,858,699,1176)")二维码
// // ocrc("前往图中包含的公众号")bounds("(480,1757,664,1819)")text("青团设计")bounds("(350,1683,730,1735)")text("前往图中包含的公众号")
// bounds("(0,1618,1080,1867)")clickable("true")

// ocrc("青团设计")bounds("(442,110,626,164)")text("青团设计")
// click(x, y)输入框bounds("(130,1910,842,2018)")clickable("true")
// paste()
// 130,1910,9242018
// 30,850,130,1910
// 0,1796,130,1910
// ocrc("发送")bounds("(896,1087,1058,1173)")上下bounds("(896,1922,1058,2008)")text("发送")
// ocrc("点我开始阅读")最后一个bounds("(570,1747,924,1870)")
// bounds("(288,1616,791,1751)")text("继续访问")

// bounds("(472,798,607,868)")text("已完成")bounds("(383,679,696,990)")圆
// bounds("(170,1028,909,1157)")text("我不喜欢这篇文章，换一篇阅读")
// bounds("(159,1187,920,1255)")text("本轮有疑似有问题的文章，点我举报")





// bounds("(67,1228,1012,1441)")公众号关注推荐
// bounds("(224,1238,531,1300)")text("公众号关注推荐")bounds("(764,1290,1012,1379)")text("开始活动")
// bounds("(353,1219,726,1284)")text("长按识别前往关注")
// bounds("(380,879,699,1201)")二维码
// bounds("(350,1683,730,1735)")text("前往图中包含的公众号")bounds("(0,1618,1080,1867)")clickable("true")
// bounds("(445,684,635,736)")text("关注公众号")bounds("(54,656,1026,764)")clickable("true")
// com.tencent.mm.plugin.profile.ui.ContactInfoUI
// bounds("(732,684,846,736)")text("发消息")bounds("(551,656,1026,764)")clickable("true")
// bounds("(221,893,858,1066)")text("本次已关注")
// bounds("(221,1133,858,1309)")text("未关注")
// com.tencent.mm.plugin.webview.ui.tools.MMWebViewUI



// bounds("(67,1438,1012,1649)")视频号关注推荐
// bounds("(224,1446,531,1508)")text("视频号关注推荐")bounds("(764,1498,1012,1587)")text("开始活动")
// bounds("(353,1041,726,1103)")text("长按识别前往关注")
// bounds("(380,698,699,1020)")二维码
// bounds("(350,1683,730,1735)")text("前往图中包含的视频号")
// bounds("(248,911,334,955)")bounds("(248,759,334,803)")text("关注")bounds("(54,879,529,987)")bounds("(54,727,529,835)")clickable("true")
// bounds("(256,759,370,803)")text("已关注")bounds("(54,727,529,835)")clickable("true")
// bounds("(221,893,858,1066)")text("本次已关注")
// bounds("(221,1133,858,1309)")text("未关注")






//提现
// bounds("(54,220,237,280)")text("16十六点")不准
// bounds("(54,277,356,326)")text("用户ID：11063285")
// bounds("(499,480,1026,534)")text("可用积分：783")
// bounds("(499,423,872,477)")text("可兑换: 0 元")准
// bounds("(866,415,1026,485)")text("兑换")
// bounds("(437,1249,591,1298)")text("积分转移")
// bounds("(118,1098,961,1206)")text("提现到微信")不准
// bounds("(72,1033,756,1109)")text("请输入对方ID")bounds("(72,1033,653,1109)")
// bounds("(666,1014,1007,1125)")text("查询昵称")
// bounds("(569,1373,912,1473)")text("确认绑定")





















// 点击第一个
// ocrc("点我开始阅读", 570, 200, 920, 1870, false);
// 点击最后一个"我"
// ocrc("点我开始阅读", 570, 200, 920, 1870, true);
// 截屏后区域内查找文字，支持点击最后一个匹配项
function ocrc(targetStr, x1, y1, x2, y2, clickLast) {
    // 处理默认参数值
    if (typeof clickLast === "undefined") {
        clickLast = false;
    }
    // 声明参考位置变量，初始为null
    let referencePosition = null;
    const maxRetries = 5;
    let retryCount = 0;
    let found = false;
    let foundPositions = []; // 存储所有找到的位置

    while (retryCount < maxRetries && !found) {
        let screenshotPath = "/sdcard/Download/screen_" + retryCount + ".png";
        captureScreen(screenshotPath);
        sleep(1000);

        let fullImg = images.read(screenshotPath);
        if (!fullImg) {
            toastLog("全屏图片读取失败，重试中...");
            sleep(5000);
            retryCount++;
            continue;
        }

        let clippedImg = images.clip(fullImg, x1, y1, x2 - x1, y2 - y1);
        fullImg.recycle();

        let clippedPath = "/sdcard/Download/clipped_" + retryCount + ".png";
        clippedImg.saveTo(clippedPath);
        sleep(500);

        let finalImg = images.read(clippedPath);
        clippedImg.recycle();

        if (!finalImg) {
            toastLog("裁剪图片读取失败，重试中...");
            sleep(5000);
            retryCount++;
            continue;
        }

        let useSlim = false;
        let ocrResults = paddle.ocr(finalImg, useSlim);
        finalImg.recycle();

        toastLog("搜索文字: " + targetStr + " (" + (retryCount + 1) + "/" + maxRetries + ")");
        foundPositions = []; // 重置位置列表

        // 收集所有匹配项
        for (let i = 0; i < ocrResults.length; i++) {
            let ocrText = ocrResults[i].text;
            if (ocrText.includes(targetStr)) {
                let bounds = ocrResults[i].bounds;

                // 计算全屏坐标的边界
                let screenBounds = {
                    left: bounds.left + x1,
                    top: bounds.top + y1,
                    right: bounds.right + x1,
                    bottom: bounds.bottom + y1
                };

                // 在边界框内生成随机点击坐标
                let randomX = random(screenBounds.left, screenBounds.right);
                let randomY = random(screenBounds.top, screenBounds.bottom);

                // 存储位置和边界信息
                foundPositions.push({
                    x: randomX,  // 随机X坐标
                    y: randomY,  // 随机Y坐标
                    bounds: screenBounds  // 全屏坐标边界
                });
            }
        }

        // 如果有找到匹配项
        if (foundPositions.length > 0) {
            found = true;
            // toastLog("找到 " + foundPositions.length + " 个" + targetStr);

            // 确定要点击的位置
            let clickPosition = null;
            if (clickLast) {
                // 找到最靠右下角的匹配项（最后一个）
                clickPosition = foundPositions[0];
                for (let i = 1; i < foundPositions.length; i++) {
                    // 优先比较垂直位置（y坐标大的在下面），再比较水平位置（x坐标大的在右边）
                    if (foundPositions[i].bounds.top > clickPosition.bounds.top ||
                        (foundPositions[i].bounds.top === clickPosition.bounds.top &&
                            foundPositions[i].bounds.left > clickPosition.bounds.left)) {
                        clickPosition = foundPositions[i];
                    }
                }
                // toastLog("选择最后一个匹配项，位置: " + clickPosition.x + "," + clickPosition.y);
                toastLog("选择最后一个文字:" + targetStr);
            } else {
                // 默认点击第一个找到的
                clickPosition = foundPositions[0];
                // toastLog("选择第一个匹配项，位置: " + clickPosition.x + "," + clickPosition.y);
                toastLog("选择第一个文字:" + targetStr);
            }

            // 保存参考位置（如果是"文章阅读推荐"）
            if (targetStr) {
                // 返回左上角坐标
                referencePosition = {
                    x: clickPosition.bounds.left,  // 左上角X坐标
                    y: clickPosition.bounds.top,   // 左上角Y坐标
                    bounds: clickPosition.bounds   // 保留边界信息
                };
            }

            // 执行点击（使用随机坐标）
            click(clickPosition.x, clickPosition.y);
            sleep(random(3000, 5000));
        } else {
            toastLog("未找到：" + targetStr + "，5秒后重试...");
            sleep(5000);
            retryCount++;
        }
    }

    if (!found) {
        toastLog("重试" + maxRetries + "次后仍未找到: " + targetStr);
    }
    // 返回参考位置（如果是"文章阅读推荐"）
    return referencePosition;
}

function ocrl(str, x1, y1, x2, y2) {
    const maxRetries = 5;
    let retryCount = 0;
    let found = false;
    while (retryCount < maxRetries && !found) {
        let screenshotPath = "/sdcard/Download/screen_" + retryCount + ".png";
        captureScreen(screenshotPath);
        sleep(1000);

        // 修改1：先读取图片再裁剪
        let fullImg = images.read(screenshotPath);
        if (!fullImg) {
            toastLog("全屏图片读取失败，重试中...");
            sleep(5000);
            retryCount++;
            continue;
        }

        // 修改2：使用图片对象进行裁剪
        let clippedImg = images.clip(fullImg, x1, y1, x2 - x1, y2 - y1);
        fullImg.recycle(); // 及时回收全屏图片

        let clippedPath = "/sdcard/Download/clipped_" + retryCount + ".png";
        clippedImg.saveTo(clippedPath);
        sleep(500);

        // 修改3：读取裁剪后的图片（使用新变量名）
        let finalImg = images.read(clippedPath);
        clippedImg.recycle(); // 回收裁剪图片

        if (!finalImg) {
            toastLog("裁剪图片读取失败，重试中...");
            sleep(5000);
            retryCount++;
            continue;
        }

        let useSlim = false;
        let ocrResults = paddle.ocr(finalImg, useSlim);
        finalImg.recycle(); // OCR完成后回收图片

        toastLog("搜索文本: " + str + " (" + (retryCount + 1) + "/" + maxRetries + ")");
        found = false;

        for (let i = 0; i < ocrResults.length; i++) {
            let ocrText = ocrResults[i].text;
            if (ocrText.includes(str)) {
                let bounds = ocrResults[i].bounds;
                let x = random(x1 + bounds.left, x1 + bounds.right);
                let y = random(y1 + bounds.top, y1 + bounds.bottom);

                toastLog("点击坐标: " + x + "," + y);
                // click(x, y);
                longClick(x, y)
                sleep(random(3000, 5000));
                found = true;
                break;
            }
        }

        if (!found) {
            toastLog("未找到" + str + "，5秒后重试...");
            sleep(5000);
            retryCount++;
        }
    }

    if (!found) {
        toastLog("重试" + maxRetries + "次后仍未找到: " + str);
    }
    return found;
}

// 特定范围内提取文字，并返回识别到的字符
function ocrf(x1, y1, x2, y2) {
    const maxAttempts = 3; // 最多尝试3次
    let attemptCount = 0;
    let allText = ""; // 保存所有识别文本

    while (attemptCount < maxAttempts) {
        toastLog("开始识别文字 #" + (attemptCount + 1) + "/" + maxAttempts);

        // 截取全屏
        let screen = captureScreen();
        if (!screen) {
            toastLog("截屏失败，3秒后重试");
            sleep(3000);
            attemptCount++;
            continue;
        }

        // 裁剪目标区域
        let clipped = images.clip(screen, x1, y1, x2 - x1, y2 - y1);
        screen.recycle();  // 回收全屏图片内存

        if (!clipped) {
            toastLog("区域裁剪失败，3秒后重试");
            sleep(3000);
            attemptCount++;
            continue;
        }

        // 执行OCR识别
        let ocrResults = paddle.ocr(clipped, false);
        clipped.recycle();  // 回收裁剪图片内存

        // 拼接所有识别文本
        allText = ""; // 重置文本
        for (let i = 0; i < ocrResults.length; i++) {
            allText += ocrResults[i].text + " ";
        }

        // 移除多余空格
        allText = allText.trim();

        // 如果有识别到文本，提前返回
        if (allText) {
            toastLog("识别到: " + allText);
            return allText;
        }

        toastLog("未识别到 " + allText + "，3秒后重试...");
        sleep(3000);
        attemptCount++;
    }

    toastLog("尝试" + maxAttempts + "次后未识别到文本");
    return ""; // 返回空字符串
}


//点击
function cck(x1, y1, x2, y2) {
    var randX = random(x1, x2);
    var randY = random(y1, y2);
    click(randX, randY)
}
//长按
function longCk(x1, y1, x2, y2) {
    var randX = random(x1, x2);
    var randY = random(y1, y2);
    longClick(randX, randY)
}

// 控件-随机点击
function randomClick(kjobj) {
    var bound = kjobj.bounds();
    // var randX = bound.left + random(1, bound.right - bound.left - 2);
    // var randY = bound.top + random(1, bound.bottom - bound.top - 2);

    var randX = random(bound.left + 1, bound.right - 1);
    var randY = random(bound.top + 1, bound.bottom - 1);
    click(randX, randY)//(0, 1904 - 216, 2040)
}

